package pms.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import pms.dao.TheaterDao;
import pms.om.Event;
import pms.om.Kunde;
import pms.om.Rechnung;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import com.sun.org.apache.xalan.internal.xsltc.compiler.Template;


/** 
 * Controller to generate the Home Page basics to be rendered by a view. 
 * It extends the convenience class AbstractController that encapsulates most 
 * of the drudgery involved in handling HTTP requests. 
 * @author KRS DEV Team (krsdevteam@PMSWIbb2011SS.de)
 */
public class SaveReservierungNow extends AlleReservierungen {
	
	protected ModelAndView handleRequestInternal(
			HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse) throws Exception {
		
		httpServletRequest.setCharacterEncoding("UTF-8");
		int kundeId = Integer.parseInt(httpServletRequest.getParameter("kundeId"));
		int eventId = Integer.parseInt(httpServletRequest.getParameter("eventId"));
			
		String name = httpServletRequest.getParameter("name");
		String vorname = httpServletRequest.getParameter("vorname");
		String strasse = httpServletRequest.getParameter("strasse");
		String plz = httpServletRequest.getParameter("plz");
		String ort = httpServletRequest.getParameter("ort");
		String tel = httpServletRequest.getParameter("tel");
		String email = httpServletRequest.getParameter("email");
		String kto = httpServletRequest.getParameter("kto");
		String blz = httpServletRequest.getParameter("blz");
		String bank = httpServletRequest.getParameter("bank");
		
		getKundeDao().create(name, vorname, strasse, plz, ort, tel, email, kto, blz, bank);
		
		List<Kunde> kunden = getKundeDao().findAll();	
		int size_kunden = kunden.size();
		size_kunden--;
		Kunde derKunde = kunden.get(size_kunden);
		
		int i1 = Integer.parseInt(httpServletRequest.getParameter("res_plaetze_loge"));
		int i2 = Integer.parseInt(httpServletRequest.getParameter("res_plaetze_reg"));
		
		
		SimpleDateFormat formatter;
		formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss");
		Date today = new Date();
		String d  = formatter.format(today);
		
		//Event e = new Event();
		
		//e.setId(eventId);			
		
		getEventDao().modifyPlaetze(eventId, i1, i2);		
		
		Kunde k = new Kunde();
		k.setId(1);
		
		Rechnung r = new Rechnung();
		r.setId(1);		
		
		getReservierungDao().create(i1, i2, d, getEventDao().findById(eventId), derKunde, r);
		

		return super.handleRequestInternal(httpServletRequest, httpServletResponse);
	}

}
